package gui.data.D2;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.PolygonCalculator;

public class PolygonInputGui extends JPanel{

	JTextField cornersInput, areaInput, sideInput, circumferenceInput, apothemInput, radiusInput;
	MyFrame myFrame;
	JLabel cornersLbl, areaLbl, sideLbl, circumferenceLbl, apothemLbl, radiusLbl;
	int guide;

	public PolygonInputGui(final MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(0, 2));
		this.setPreferredSize(new Dimension(500, 500));
		
		cornersLbl = new JLabel("Number of corners = ");
		areaLbl = new JLabel("Area = ");
		sideLbl = new JLabel("Side length = ");
		circumferenceLbl = new JLabel("Circumference = ");
		apothemLbl = new JLabel("Apothem = ");
		radiusLbl = new JLabel("Radius = ");
		
		cornersInput = new JTextField();
		areaInput = new JTextField();
		sideInput = new JTextField();
		circumferenceInput = new JTextField();
		apothemInput = new JTextField();
		radiusInput = new JTextField();
		
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		
		this.add(cornersLbl);
		this.add(cornersInput);
		this.add(sideLbl);
		this.add(sideInput);
		this.add(apothemLbl);
		this.add(apothemInput);
		this.add(circumferenceLbl);
		this.add(circumferenceInput);
		this.add(areaLbl);
		this.add(areaInput);
		this.add(radiusLbl);
		this.add(radiusInput);
		this.add(new JLabel(""));
		this.add(calculate);		
	}

	private void openResult() {
		boolean error = false;
		JTextField[] textFields = {cornersInput, sideInput, apothemInput, radiusInput, circumferenceInput, areaInput};
		Double[] values = new Double[7];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new PolygonCalculator(values[0],values[1],values[2],values[3],values[4], values[5],  myFrame);
		}
	}
}
